package com.example.seey2023.utils;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IPUtil {

    /**
     * 获取外网ip
     *
     * @return
     */
    public static String getV4IP() {
        String ip = "";
        String chinaz = "https://ip.chinaz.com";

        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(chinaz);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            while ((read = in.readLine()) != null) {
                inputLine.append(read + "\r\n");
            }
            //System.out.println(inputLine.toString());
            /**
             * <dd class="fz24">120.231.97.201</dd>
             * <dd>中国广东中山 移动</dd>
             */
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
        Matcher m = p.matcher(inputLine.toString());
        if (m.find()) {
            String ipstr = m.group(1);
            ip = ipstr;
            //System.out.println(ipstr);
        }
        return ip;
    }

    public static String getV4IP2() {
        String ip = "";
        String city = "";
        String chinaz = "https://ip.chinaz.com";

        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(chinaz);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            while ((read = in.readLine()) != null) {
                inputLine.append(read + "\r\n");
            }
//            System.out.println(inputLine.toString());
            /**
             * <dd class="fz24">120.231.97.201</dd>
             * <dd>中国广东中山 移动</dd>
             */
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
        Matcher m = p.matcher(inputLine.toString());
//        System.out.println(m);
        if (m.find()) {
            String ipstr = m.group(1);
            ip = ipstr;
            //System.out.println(ipstr);
        }

        Pattern cityp = Pattern.compile("\\<dd>(.*?)\\<\\/a>");
        Matcher citym = cityp.matcher(inputLine.toString());
//        System.out.println(citym);
        if (citym.find()) {
            String citystr = citym.group(1);


            city = citystr.substring(0, citystr.length() - 84);
            //System.out.println(ipstr);
            // System.out.println(citym.group(1));//中国广东中山 移动<a href="http://tool.chinaz.com/contact" target="_blank" class="col-blue02 pl5">(纠错)
        }

        return ip + city;
    }

    public static String getV4IP3() {
        String ip = "";
        String city = "";
        String chinaz = "https://ip.chinaz.com";

        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(chinaz);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            while ((read = in.readLine()) != null) {
                inputLine.append(read + "\r\n");
            }
//            System.out.println(inputLine.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
        Matcher m = p.matcher(inputLine.toString());
//        System.out.println(m);
        if (m.find()) {
            String ipstr = m.group(1);
            ip = ipstr;
            //System.out.println(ipstr);
        }

        Pattern cityp = Pattern.compile("\\<dd>(.*?)\\<\\/a>");
        Matcher citym = cityp.matcher(inputLine.toString());
//        System.out.println(citym);
        if (citym.find()) {
            String citystr = citym.group(1);

            city = citystr.substring(0, citystr.length() - 86);
            // System.out.println(citym.group(1));//中国广东中山 移动<a href="http://tool.chinaz.com/contact" target="_blank" class="col-blue02 pl5">(纠错)
        }

        return city;
    }


}
